<!--
  * 一级项目清单
-->
<template>
    <div v-if="isShow">
        <!---------- 查询表单form end ----------->
        <a-card size="small" :bordered="false" :hoverable="true">
            <!---------- 表格操作行 begin ----------->
            <a-row class="smart-table-btn-block">
                <div class="smart-table-operate-block" style="width: 90%;">
                    <a-button @click="down(1)" type="primary" :loading="iconLoading">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        一级项目导出
                    </a-button>
                    <a-button @click="down(2)" type="primary" :loading="iconLoadingTwo">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        二级项目导出
                    </a-button>
                    <a-button @click="down(3)" type="primary" :loading="iconLoadingThree">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        年度预算导出
                    </a-button>
                    <a-button @click="down(4)" type="primary" :loading="iconLoadingFour">
                        <template #icon>
                            <UsbOutlined />
                        </template>
                        资金使用申请导出
                    </a-button>
                </div>
                <span class="smart-table-column-operate">
                    <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.SYSTEM.EMPLOYEE" :refresh="queryData" />
                </span>
            </a-row>
            <!---------- 表格操作行 end ----------->

            <!---------- 表格 begin ----------->
            <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="blfunduseid" bordered
                :loading="tableLoading" :pagination="false" :scroll="{ x: 1500}">
                <template #bodyCell="{ text, record, column }">
                    <template v-if="column.dataIndex === 'blprojectname'">
                        <div style="color: #1878ff;" @click="showFormView(record)" type="link">{{record.blprojectname}}
                        </div>
                    </template>
                    <template v-if="column.dataIndex === 'goSecond'">
                        <div class="smart-table-operate">
                            <a style="color: #1878ff;" v-show="record.SedProject"
                                @click="goSecond(record.SedProject)">查看二级项目</a>
                            <span type="link" v-show="!record.SedProject">暂无二级项目</span>
                        </div>
                    </template>
                    <template v-if="column.dataIndex === 'blprojectstatus'">
                        <div class="smart-table-operate">
                            <span type="link">{{ record.blprojectstatus == 0
                                ? "项目立项"
                                : record.blprojectstatus == 1
                                ? "跟进阶段"
                                : record.blprojectstatus == 2
                                ? "投标阶段"
                                : record.blprojectstatus == 3
                                ? "项目结项"
                                : "废弃项目"}}</span>
                        </div>
                    </template>
                    <!--  -->
                </template>
            </a-table>
            <!---------- 表格 end ----------->
            <!-- 分页-->
            <div class="smart-query-table-page">
                <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
                    :defaultPageSize="queryForm.limit" v-model:current="queryForm.page"
                    v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData"
                    :show-total="(total) => `共${total}条`" />
            </div>
        </a-card>
        <NameOperate ref="formRef" @refresh="queryData" />
    </div>
    <!-- 二级项目页面-->
    <SedProVue v-else :passParams="fuPassword" @emitPass="changeShow"></SedProVue>

</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import NameOperate from '../../BasicManage/ProManage/modules/Name-operate.vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import { getStatisticsApi } from '/@/api/infoStatistics/infoStatistics-api';
    import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import SedProVue from './modules/SedVue.vue';
    import _ from 'lodash';
    import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    // ---------------------------- 表格列 ----------------------------
    const columns = ref([
        {
            title: '项目名称',
            dataIndex: 'blprojectname',
            fixed: 'left',
            align: 'center',
            width: 140,
        },
        {
            title: '项目编号',
            dataIndex: 'blprojectnum',
            align: 'center',
        },
        // {
        //     title: '部门名称',
        //     dataIndex: 'blagreementnum',

        // },
        {
            title: '财务编号',
            dataIndex: 'blfinancialnum',
            align: 'center',
        },
        {
            title: '项目联系人',
            dataIndex: 'blprojectcontact',
            align: 'center',
        },
        {
            title: '项目负责人',
            dataIndex: 'blprojectleader',
            align: 'center',
        },
        {
            title: '创建时间',
            dataIndex: 'blregist',
            align: 'center',
        },
        {
            title: '二级项目入口',
            dataIndex: 'goSecond',
            align: 'center',
        },
        {
            title: '项目状态',
            dataIndex: 'blprojectstatus',
            align: 'center',
        },
    ]);
    // ---------------------------- 查询数据表单和方法 ----------------------------

    const queryFormState = {
        page: 1,
        limit: PAGE_SIZE,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 查看  
    const formRef = ref();
    function showFormView(data) {
        const newObj = Object.assign(data, { showView: true });
        formRef.value.showViewModal(newObj);
    }
    // 进入二级项目

    function goSecond(data) {
        isShow.value = false;
        fuPassword.value = data;
    }
    // ---------------------------- 查询数据表单和方法 ----------------------------

    // 表格加载loading
    const tableLoading = ref(false);
    // 按钮加载loading
    const iconLoading = ref(false);
    const iconLoadingTwo = ref(false);
    const iconLoadingThree = ref(false);
    const iconLoadingFour = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);
    // 总金额
    const totalMoney = ref(0);
    // 控制父组件是否展示
    const isShow = ref(true);
    // 改变父组件展示状态
    function changeShow() {
        isShow.value = true;
    }
    // ---------------------------- 详情 ----------------------------
    const fuPassword = ref({});

    function viewDetail(data) {
        isShow.value = false;
        fuPassword.value = data;
    }

    // 查看年度协议
    function viewAnnual(data) {
        isShow.value = 2;
    }
    // ====== 导出============= 
    async function down(value) {

        try {
            if (value == 1) {
                iconLoading.value = true;
                const queryResult = await getStatisticsApi.exportProList();
                // 创建a标签
                const link = document.createElement('a')
                const url = window.URL.createObjectURL(new Blob([queryResult.data]))
                // 指定下载文件名
                link.download = '一级项目统计表.xlsx'
                // 指定下载链接
                link.href = url
                // 把a标签加到页面中
                document.body.appendChild(link)
                // 触发a标签下载
                link.click()
                // 下载完成移除元素
                document.body.removeChild(link)
                // 释放掉blob对象
                window.URL.revokeObjectURL(url)
                message.success(`导出成功！`)
            }
            if (value == 2) {
                iconLoadingTwo.value = true;
                const queryResult = await getStatisticsApi.exportProTwoList();
                // 创建a标签
                const link = document.createElement('a')
                const url = window.URL.createObjectURL(new Blob([queryResult.data]))
                // 指定下载文件名
                link.download = '二级项目统计表.xlsx'
                // 指定下载链接
                link.href = url
                // 把a标签加到页面中
                document.body.appendChild(link)
                // 触发a标签下载
                link.click()
                // 下载完成移除元素
                document.body.removeChild(link)
                // 释放掉blob对象
                window.URL.revokeObjectURL(url)
                message.success(`导出成功！`)
            }
            if (value == 3) {
                iconLoadingThree.value = true;
                const queryResult = await getStatisticsApi.exportBudgetList();
                // 创建a标签
                const link = document.createElement('a')
                const url = window.URL.createObjectURL(new Blob([queryResult.data]))
                // 指定下载文件名
                link.download = '年度预算统计表.xlsx'
                // 指定下载链接
                link.href = url
                // 把a标签加到页面中
                document.body.appendChild(link)
                // 触发a标签下载
                link.click()
                // 下载完成移除元素
                document.body.removeChild(link)
                // 释放掉blob对象
                window.URL.revokeObjectURL(url)
                message.success(`导出成功！`)
            }
            if (value == 4) {
                iconLoadingFour.value = true;
                const queryResult = await getStatisticsApi.exportApplyList();
                // 创建a标签
                const link = document.createElement('a')
                const url = window.URL.createObjectURL(new Blob([queryResult.data]))
                // 指定下载文件名
                link.download = '资金使用申请统计表.xlsx'
                // 指定下载链接
                link.href = url
                // 把a标签加到页面中
                document.body.appendChild(link)
                // 触发a标签下载
                link.click()
                // 下载完成移除元素
                document.body.removeChild(link)
                // 释放掉blob对象
                window.URL.revokeObjectURL(url)
                message.success(`导出成功！`)
            }

        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            iconLoading.value = false;
            iconLoadingTwo.value = false;
            iconLoadingThree.value = false;
            iconLoadingFour.value = false;
        }
    }

    // 查询数据
    async function queryData() {
        tableLoading.value = true;
        try {
            let queryResult = await getStatisticsApi.getOnePro(queryForm);
            const data = queryResult.data
            const handleData = data.map(item => {
                if (item.children) {
                    return {
                        parent: {
                            ...item.parent,
                            SedProject: item.children
                        }
                    };
                } else {
                    return item;
                }
            });
            const newData = handleData.map(item => item.parent);
            tableData.value = newData;
            total.value = queryResult.total;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }

    onMounted(queryData);
</script>